﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.Data;
using VINETLIFE.AdminCP;
using System.Web.UI.HtmlControls;

namespace VINETLIFE.Customers
{
    public partial class ClientDetail : System.Web.UI.Page
    {
        cmsCustomerDO objCus = new cmsCustomerDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CustomerId"] != null)
            {
                objCus.Id = int.Parse(Session["CustomerId"].ToString());
                if (!IsPostBack)
                {
                    SEO();
                    InitForm();
                }
            }
            else
            {
                Functions.Alert("Bạn không có quyền truy cập vào khi chưa đăng nhập, vui lòng đăng nhập", "/dang-nhap/?ReturnUrl=he-thong-khach-hang");
            }
        }
       public string Status(object e)
       {
           if (e == null)
           {
               return "---";
           }
           else
           {
               if (e.ToString().Equals("OK"))
               {
                   return "Đúng";
               }
               else
               {
                   return "Sai";
               }
           }
       }
        private void SEO()
        {
            Page.Title = "Chi tiết khách hàng | " + Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);


            objConfig.ConfigID = 3;
            objConfig = new sysConfigBL().Select(objConfig);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaDesc);
        }
        private void InitForm()
        {
            DateTime now = DateTime.Now;
            DateTime fromDate = new DateTime(now.Year, now.Month, 1);
            DateTime toDate = new DateTime(now.Year, now.Month, 30);
            if (!txtFromDate.Text.Trim().Equals("") && !txtToDate.Text.Trim().Equals(""))
            {
                fromDate = Myfunction.ReturnValidateDateTime(txtFromDate.Text.Trim());
                toDate = Myfunction.ReturnValidateDateTime(txtToDate.Text.Trim());
            }
            int customerId = objCus.Id;
            grvAccountReceivedMoney.DataSource = new cmsAccountReceiveMoneyBL().Statistic(fromDate, toDate, customerId);
            grvAccountReceivedMoney.DataBind();
        }


        protected void btnHuy_Click(object sender, EventArgs e)
        {
            InitForm();
        }

        protected void grvAccountReceivedMoney_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvAccountReceivedMoney.PageIndex = e.NewPageIndex;
            InitForm();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            InitForm();
        }
        long totalValue = 0;
        protected void grvAccountReceivedMoney_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Label lblReceivedMoney = (Label)e.Row.FindControl("lblReceivedMoney");
                long qty = long.Parse(lblReceivedMoney.Text.Replace(",",""));
                totalValue = totalValue + qty;
            }

            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblTotalCount = (Label)e.Row.FindControl("lblTotalCount");
                lblTotalCount.Text = "Tổng tiền: " + string.Format("{0:0,##}",totalValue);

            }
        }
    }
}
